import { NativeModules, Platform } from 'react-native';

// eslint-disable-next-line import/no-cycle
import { Permission } from '../../constant';

const isIOS = Platform.OS === 'ios';
let aliPayInstalled = true;

export interface ScreenPhysicalPixels {
  width: number;
  height: number;
}

export interface MemoryInfo {
  availMemory: string;
  totalMemory: string;
}

const getScreenPhysicalPixels = (): ScreenPhysicalPixels =>
  NativeModules.SystemUtil.getScreenPhysicalPixels();

const getMemoryInfo = (): MemoryInfo =>
  NativeModules.SystemUtil.getMemoryInfo();

const getOSVersion = (): string => NativeModules.SystemUtil.getOSVersion();

const getCpuName = (): string => NativeModules.SystemUtil.getCpuName();

const getNumberOfCPUCores = (): number =>
  NativeModules.SystemUtil.getNumberOfCPUCores();

const getDeviceProduct = (): string =>
  NativeModules.SystemUtil.getDeviceProduct();

const getDeviceModel = (): string => NativeModules.SystemUtil.getDeviceModel();

const getPackageId = (): string => NativeModules.SystemUtil.getPackageId();

const getVersionChannel = (): string =>
  NativeModules.SystemUtil.getVersionChannel();

const getAppVersion = (): string => NativeModules.SystemUtil.getAppVersion();

const getAppDeviceId = (): string => NativeModules.SystemUtil.getAppDeviceId();

const hasNotchInScreen = (): boolean =>
  NativeModules.SystemUtil.hasNotchInScreen();

const getNotchWidth = (): number => NativeModules.SystemUtil.getNotchWidth();

const getBottomSafeAreaHeight = (): number => {
  if (isIOS) return NativeModules.SystemUtil.getBottomSafeAreaHeight();
  return 0;
};

const getCodePushKey = (): string => NativeModules.SystemUtil.getCodePushKey();

const getVersionCode = (): number => NativeModules.SystemUtil.getVersionCode();

const exitApp = (): void => NativeModules.SystemUtil.exitApp();

const hideNavigationBar = (): void => {
  if (!isIOS) {
    NativeModules.SystemUtil.hideNavigationBar();
  }
};

const showNavigationBar = (): void => {
  if (!isIOS) {
    NativeModules.SystemUtil.showNavigationBar();
  }
};

const openSetting = (cb: () => void): void => {
  if (isIOS) {
    NativeModules.SystemUtil.openSetting();
  } else {
    NativeModules.SystemUtil.openSetting(cb);
  }
};

const checkAliPayInstalled = (): boolean => {
  if (isIOS) {
    return aliPayInstalled;
  }
  return NativeModules.SystemUtil.checkAliPayInstalled();
};

const getAliPayInstalled = (): void => {
  if (isIOS) {
    NativeModules.SystemUtil.checkAliPayInstalled((res: boolean): void => {
      aliPayInstalled = res;
    });
  }
};

const getIMEI = (): string => {
  if (!isIOS) {
    if (NativeModules.SystemUtil.getIMEI) {
      return NativeModules.SystemUtil.getIMEI();
    }
  }
  return '';
};

const getIDFA = (): Promise<string> =>
  new Promise((resolve, reject): void => {
    if (!isIOS) {
      resolve('');
    }
    if (NativeModules.SystemUtil.getIDFAAsync) {
      NativeModules.SystemUtil.getIDFAAsync((res: string): void => {
        resolve(res);
      });
    } else if (NativeModules.SystemUtil.getIDFA) {
      const idfa = NativeModules.SystemUtil.getIDFA();
      resolve(idfa);
    }
  });

const hideVolumeView = (hide: boolean): void => {
  if (isIOS) return;
  NativeModules.SystemUtil.hideVolumeView(hide);
};

const setIsSettingMediaVolume = (isSetting: boolean): void => {
  if (isIOS) return;
  NativeModules.SystemUtil.setIsSettingMediaVolume(isSetting);
};

const getIsTabletDevice = (): boolean => {
  return NativeModules.SystemUtil.getIsTabletDevice();
};

const isFaceDetectedSupported = (): boolean => {
  if (isIOS) return true;
  if (NativeModules.SystemUtil.isFaceDetectedSupported) {
    return NativeModules.SystemUtil.isFaceDetectedSupported();
  }
  return true;
};

const checkPermission = (
  permission: string
): Promise<{ [key: string]: Permission }> => {
  if (
    isIOS ||
    !NativeModules.SystemUtil.checkPermission ||
    permission.length === 0
  ) {
    return new Promise((resolve, reject): void => {});
  }
  return NativeModules.SystemUtil.checkPermission(permission);
};

export default {
  getScreenPhysicalPixels,
  getMemoryInfo,
  getOSVersion,
  getCpuName,
  getNumberOfCPUCores,
  getDeviceProduct,
  getDeviceModel,
  getPackageId,
  getVersionChannel,
  getAppVersion,
  getAppDeviceId,
  hasNotchInScreen,
  getNotchWidth,
  getCodePushKey,
  getVersionCode,
  exitApp,
  hideNavigationBar,
  showNavigationBar,
  openSetting,
  checkAliPayInstalled,
  getAliPayInstalled,
  getBottomSafeAreaHeight,
  getIMEI,
  getIDFA,
  hideVolumeView,
  setIsSettingMediaVolume,
  getIsTabletDevice,
  checkPermission,
  isFaceDetectedSupported
};
